package com.sp.business.normalization.impl;

import java.util.UUID;

import org.apache.commons.lang.StringUtils;

import com.sp.generated.xml.firemon.NATRule;
import com.sp.generated.xml.firemon.SecurityRule;
import com.sp.generated.xml.firemon.SecurityRuleCollection;

public abstract class SecurityRuleKeyGenerator
{
  public abstract void generateSecurityRuleKeys(SecurityRuleCollection previous, SecurityRuleCollection current);

  protected static String copyUID(SecurityRule previous, SecurityRule current)
  {
    String guid = null;

    if (previous != null)
      guid = previous.getUID();

    if (StringUtils.isBlank(guid) || !NormalizationUtil.isStringUUID(guid))
      guid = String.format("{%s}", UUID.randomUUID().toString().toUpperCase());

    current.setUID(guid);
    current.setXsid("SecurityRule" + guid.replaceAll("\\{", "").replaceAll("\\}", ""));

    //if security rule contains nat Rule
    copyEmbededNatRuleUID(previous, current);
    
    return guid;
  }
  
  protected static void copyEmbededNatRuleUID(SecurityRule previousSecurityRule, SecurityRule securityRule)
  {
	//check if security rule contains natRule, match and copy natRule guid if applicable
      NATRule natRule = securityRule.getNatRule();
      NATRule prevNatRule = null;
      if (previousSecurityRule != null )
    	  prevNatRule = previousSecurityRule.getNatRule();
      if(securityRule.getNatRule()!=null)
      {    	
    	boolean matched = BaseNatRuleKeyGenerator.isNATRuleExactMatch(prevNatRule, natRule);
        if(matched==true)
        	NormalizationUtil.copyUID(prevNatRule, natRule);
        else
        	NormalizationUtil.copyUID(null, natRule);         
      }
  }

}
